  #include <hidef.h>      /* common defines and macros */
  #include "derivative.h"      /* derivative-specific definitions */
  #include "termio.h"
  #include "stdio.h"

  int row[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  int out[8];
  int cnt, cycles;
  
  #define LEFT 0
  #define RIGHT 1
  #define UP 2
  #define DOWN 3
  
  void moveCursor(int dir);
  
  void output(void);
  
  void draw(void);
  void erase(void);
  
  typedef struct cursor{
    char x;
    int y;
    int value;
  }cursor;
  
  struct cursor cursor_pos;
  
  void main(void) {
    /* put your own code here */
    cnt = 0;
    cursor_pos.x=4;
    cursor_pos.y=4;
    
    cursor_pos.value = 0x01 << cursor_pos.x;

  	EnableInterrupts;
    DDRA = 0xff;
    DDRB = 0xff;
    DDRT = 0x00;
   

    for(;;) {
    
    if(PTT & (0x80)){
      while(PTT & (0x80));
      moveCursor(LEFT);
    }
    
     if(PTT & (0x40)){
      while(PTT & (0x40));
      moveCursor(RIGHT);
    }
    
     if(PTT & (0x20)){
      while(PTT & (0x20));
      moveCursor(UP);
    }
    
     if(PTT & (0x10)){
      while(PTT & (0x10));
      moveCursor(DOWN);
    }
    
     if(PTT & (0x08)){
      while(PTT & (0x08));
      draw();                            
    }
    
     if(PTT & (0x04)){
      while(PTT & (0x04));
      erase();
    }
    
   output();
      
     
      _FEED_COP(); /* feeds the dog */
      
    } /* loop forever */
    /* please make sure that you never leave main */
  }
  
  void output(void){
    if(cycles == 1000){
        cursor_pos.value ^=  (0x01 << cursor_pos.x);
        cycles = 0;

    }
    if(cnt == cursor_pos.y){
        out[cnt] = row[cnt] ^ cursor_pos.value;
    }
    else{
         out[cnt] = row[cnt];
    }
  
    PORTA = 0 | (1 << cnt);
    PORTB = ~out[cnt];
    
    cnt = ((++cnt) % 8);
    if(cnt == 7){
      cycles++;
    }
    PORTA = 0;
    PORTB = 0xff;
  }
   
  void moveCursor(int dir){
       switch(dir){
        case LEFT: cursor_pos.x--; 
                   if(cursor_pos.x < 0)
                      cursor_pos.x = 0;
                   cursor_pos.value = 0x01 << cursor_pos.x;
                   break;
        case RIGHT: cursor_pos.x++;
                    if(cursor_pos.x > 7)
                      cursor_pos.x = 7;
                    cursor_pos.value = 0x01 << cursor_pos.x;
                    break;
        case UP: cursor_pos.y--;
                  if(cursor_pos.y < 0)
                    cursor_pos.y = 0;
                  cursor_pos.value = 0x01 << cursor_pos.x;
                  break;
        case DOWN: cursor_pos.y++;
                  if(cursor_pos.y > 7)
                    cursor_pos.y = 7;
                  cursor_pos.value = 0x01 << cursor_pos.x;
                  break;
        default: break;
       }
  }
  
  void draw(void){
    row[cursor_pos.y] |= (0x01 << cursor_pos.x);
  }
  
  void erase(void){
    row[cursor_pos.y] &= ~(0x01 << cursor_pos.x);
  }
    
    